package cn.by.wms.service;

import cn.by.wms.dao.IoItemDao;
import cn.by.wms.dao.PoMasterDao;
import cn.by.wms.dto.InventoryOptionDTO;
import cn.by.wms.dto.PoStatusDto;
import cn.by.wms.enums.PoStatus;
import cn.by.wms.event.InventoryOptionEvent;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.smart.core.exception.BizException;
import cn.smart.core.util.UserSessionContext;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import cn.by.wms.entity.*;
import cn.by.wms.dao.IoMasterDao;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 入库单头;(206_io_master)表服务实现类
 * @author : heimi
 * @date : 2024-11-28
 */
@Service
public class IoMasterService {
     
    @Autowired
    private IoMasterDao ioMasterDao;
    @Autowired
    private PoMasterDao poMasterDao;
    @Autowired
    private IoItemDao ioItemDao;
    @Autowired
    private InventoryService inventoryService;
    //Spirng提供的发布器
    @Autowired
    private ApplicationEventPublisher publisher;
    
    public List<IoMaster> select(IoMasterQuery query) {
        return ioMasterDao.select(query);
    }
    
    public Integer insert(IoMaster ioMaster) {
        //生成入库单的Id
        String ioId="IO"+ DateUtil.format(DateUtil.date(),"yyyyMMddHHmmssSSS");
        ioMaster.setId(ioId);
        //判断这个采购库单是否存在
        String poId = ioMaster.getPoId();
        PoMasterQuery poMasterQuery = new PoMasterQuery().setId(poId);
        List<PoMaster> poMasters = poMasterDao.select(poMasterQuery);
        if (ObjectUtil.isEmpty(poMasters)){
           BizException.throwBizException(454,"采购单不存在");
        }
        //判断采购单和供应商是否匹配
        PoMaster poMaster = poMasters.get(0);
        if (poMaster.getVendorId() != ioMaster.getVendorId() || !poMaster.getVendorName().equals(ioMaster.getVendorName())){
            BizException.throwBizException(454,"采购单和供应商不匹配");
        }

        //为明细插入ioid和最后更新人
        List<IoItem> ioItems = ioMaster.getIoItems();
        ioItems.forEach(ioItem -> {
            ioItem.setIoId(ioId);
            ioItem.setLastUpdateBy(UserSessionContext.get().getNickName());
        });
        //执行事务插入
        IoMasterService ioMasterService = (IoMasterService) (AopContext.currentProxy());
        ioMasterService.addIO(ioMaster);
        return 0;
    }
    @Transactional
    public void  addIO(IoMaster ioMaster){
        ioMasterDao.insert(ioMaster);
        poMasterDao.update(new PoStatusDto().setId(ioMaster.getPoId()).setStatus(PoStatus.FINISH.getCode()));
        ioItemDao.insertBatch(ioMaster.getIoItems());
        //进行库存操作
        InventoryOptionDTO inventoryOptionDTO = new InventoryOptionDTO().setOptionType("in").setRefId(ioMaster.getId());
        InventoryOptionEvent inventoryOptionEvent = new InventoryOptionEvent(inventoryOptionDTO);
        publisher.publishEvent(inventoryOptionEvent);

    }
    public Integer update(IoMaster ioMaster) {
        return ioMasterDao.update(ioMaster);
    }
    
    public Integer delete(Integer id) {
        return ioMasterDao.delete(id);
    }
    
 
}